import { _decorator, color, director, System } from 'cc';
import { lobby } from './UIRoot';
import { BaseManager } from '../../scripts/libs/rxjs/cc3/BaseManager';
import { ReactiveProperty } from '../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { LobbySceneCanvas } from './LobbySceneCanvas';
import { MergeASAP } from '../../scripts/libs/rxjs/cc3/Merge';
import { Observable, of, Subscription } from 'rxjs';
import { FrontTipPanel } from './ui/FrontTipPanel/FrontTipPanel';
import { GameAssetsRefs } from './GameAssetsRefs/GameAssetsRefs';
import { FlowStatus } from './Enums';
import { gm } from '../../scripts/managers/GameManager';
import { GameKey } from '../../scripts/managers/SubgameConfig';
import { am } from '../../scripts/managers/AudioManager';
import { nullifySubscription } from '../../scripts/libs/rxjs/utils';
import { rxccconfig } from '../../scripts/libs/rxjs/cc3/RxCCConfig';
const { ccclass, property } = _decorator;

@ccclass('LobbyManager')
class LobbyManager extends BaseManager {

    public static instance: LobbyManager;
    static ID = 'lobby_system';
    get logColorOverride() { return color().fromHEX('#223344') };

    static registerSystem() {
        director.unregisterSystem(director.getSystem(LobbyManager.ID));
        director.registerSystem(LobbyManager.ID, lm, System.Priority.MEDIUM);
        lm.init();
    }

    static unregisterSystem() {
        lm.destroy();
        director.unregisterSystem(director.getSystem(LobbyManager.ID));
    }

    // ui flow
    flowStatus = ReactiveProperty.Create(FlowStatus.Empty);
    loadingEnabled = ReactiveProperty.Create(null);
    frontTipEnabled = ReactiveProperty.Create(false);
    frontTipReady = ReactiveProperty.Create(false);
    loadingAssets = ReactiveProperty.Create(true);
    hotupdateing = ReactiveProperty.Create(false);

    lobbySceneCanvas: LobbySceneCanvas = null;
    uiRoot: lobby.UIRoot = null;

    // fronttip
    frontTip: FrontTipPanel = null;
    // GameAssetsRefs
    gameAssetsRefs: GameAssetsRefs = null;

    _sub_buttonClick: Subscription = null;

    useObserves() {
        return [
            MergeASAP(
                this.loadingAssets, this.hotupdateing /*, this.wsConnected*/
            ).subscribe(_ => {
                this.loadingEnabled.value = [this.loadingAssets.value, this.hotupdateing.value/*, !this.wsConnected.value*/].some(_ => _);
            }),
            gm.loadingAssets.subscribe(_ => { this.loadingAssets.value = _ }),
            gm.curSubgame.subscribe(_ => {
                if (_ == null) return;
                nullifySubscription.call(this, '_sub_buttonClick');
                if (_ == GameKey.lobby) {
                    this.flowStatus.value = FlowStatus.Lobby;
                    am.bgm.value = 'ab_lobby/Audio/bgm/music';
                    this._sub_buttonClick = rxccconfig.buttonWillClickCommand.subscribe(_ => {
                        if (_.node.name.startsWith('back')) {
                            am.play.execute('ab_lobby/Audio/sfx/Back');
                        } else {
                            am.play.execute('ab_lobby/Audio/sfx/Tap');
                        }
                    });
                } else {
                    this.flowStatus.value = FlowStatus.Empty;
                    am.bgm.value = null;
                }
            }),
        ]
    }

    // 封装一个简便的信息弹出
    frontTipShow(message: string, interval: number = 2): Observable<any> {
        if (this.frontTip != null)
            return this.frontTip.show(message, interval);
        return of(false);
    }
}

globalThis.LobbyManager = LobbyManager;

export const lm = LobbyManager.instance = new LobbyManager();
